<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div id="rightinfo" class="rightinfo"></div>

<!-- 模板 -->
<script id="list" type="text/html">
    <table class="tablelist">
        <thead>
        <tr>
        <th style="width:8%">编号</th>
        <th>标题</th>
        <th style="width:10%">用户</th>
        <th style="width:10%">电话</th>
        <th style="width:15%">留言时间</th>
        <th style="width:8%">是否阅读</th>
        <th style="width:10%">操作</th>
        </tr>
        </thead>
        <tbody>
        <%for(i = 0; i < list.length; i ++) {%>
            <tr <%=#(i%2 ==0) ?'':'class="odd"'%> data-id="<%=list[i].id%>">
            <td><%=list[i].id%></td>
            <td><%=sbuStr(list[i].title,35)%></td>
            <td><%=sbuStr(list[i].nickname,4,'')%></td>
            <td><%=showTel(list[i].tel)%></td>
            <td><%=dateFormat(list[i].add_time,'yyyy-MM-dd hh:mm')%></td>
            <td><%=#list[i].status?'<i>已阅</i>':'未阅'%></td>
            <td>
                <a class="tablelink act_view">查看</a>
                <a class="tablelink act_delete">删除</a>
            </td>
            </tr>
        <%}%>        
        </tbody>
    </table>   
    <div class="pagin">
        <div class="message">共<i class="blue"><%=record_count%></i>条记录，当前显示第&nbsp;<i class="blue"><%=page_num%>/<%=page_count%></i>&nbsp;页</div>
        <div class="paginList" id="page_bar"></div>
    </div>
    <div class="clearfloat"></div>
</script>

<script id="detail" type="text/html">
<div class="showdetail">
<table>
<thead>
        <tr>
        <th style="width:20%"></th>
        <th></th>
        </tr>
</thead>
<tbody>
<tr><td class="key">标题：</td><td class="value"><%=title%></td></tr>
<tr><td class="key">内容：</td><td class="value"><%=#content%></td></tr>
<tr><td class="key">联系人：</td><td class="value"><%=nickname%>（<%=sex?'女士':'先生'%>）</td></tr>
<tr><td class="key">联系电话：</td><td class="value"><%=tel%></td></tr>
<tr><td class="key">电子邮箱：</td><td class="value"><%=email%></td></tr>
<tr><td class="key">QQ号码：</td><td class="value"><%=qq%></td></tr>
<tr><td class="key">联系地址：</td><td class="value"><%=address%></td></tr>
<tr><td class="key">公司/单位：</td><td class="value"><%=company%></td></tr>
<tr><td class="key">留言时间：</td><td class="value"><%=dateFormat(add_time,'yyyy-MM-dd hh:mm:ss')%></td></tr>
</tbody>
</table>
</div>
</script>

<!-- 脚本 -->
<script src="js/template-helper.js" type="text/javascript"></script>
<script type="text/javascript">
function get_page(){
    $.getJSON(
        '<?php echo site_url('ajax/guestbook_handle/get_list');?>',
        {
              begin: (current_status.page_num - 1) * current_status.page_size,
              length: current_status.page_size
          },
          function(rs){
              if(rs.err===0){
                current_status.page_count = Math.ceil(rs.data.record_count/current_status.page_size);
                //呈现内容
                $('#rightinfo').html(template('list', $.extend({},current_status,rs.data)));
                //显示分页
                laypage({
                    cont: 'page_bar', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                    pages: current_status.page_count, //通过后台拿到的总页数
                    curr: current_status.page_num, //当前页
                    jump: function(obj, is_self){ //触发分页后的回调
                        if(!is_self){ //点击跳页触发函数自身，并传递当前页：obj.curr
                            current_status.page_num = obj.curr;
                            get_page();
                        }
                    }
                });
              }else if(rs.err===4){
                  //数据实际页数减少，但前端没有刷新，仍旧请求不存在的页码，返回的是“列表为空”的错误
                  current_status.page_num --;
                  get_page();
              }else{
                  layer.msg(rs.msg, {icon:2,time:2000});
              }            
        }
    );
};
//加载第一页
var current_status = { page_num:1, page_size:20, page_count:1 };
get_page();

//列表操作事件
$('#rightinfo').on('click', '.act_view',function(){
    var id = $(this).parent().parent().attr('data-id');
    $.getJSON(
            '<?php echo site_url('ajax/guestbook_handle/get');?>',
            {
                id:id
            },
            function(rs){
                if(rs.err===0){
                    layer.open({
                        type: 1,
                        title: '留言信息',
                        area: ['600px', '400px'],
                        content: template('detail', rs.data),
                        cancel: function(index){
                            get_page();
                        }
                    });   
                }else{
                      layer.msg(rs.msg, {icon:2,time:2000});
                }
            }
    );
});
$('#rightinfo').on('click', '.act_delete',function(){
    var id = $(this).parent().parent().attr('data-id');
    $.post(
            '<?php echo site_url('ajax/guestbook_handle/modify');?>',
            {
                id:id,
                deleted:1
            },
            function(rs){
                if(rs.err===0){
                    layer.msg('删除成功！', {icon:1,time:2000});    
                    get_page();
                }else{
                    layer.msg(rs.msg, {icon:2,time:2000});
                }
            },
            'json'
    );
});
</script>